package com.dorm.mapper;

import com.dorm.entity.DisciplinaryRecord;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface DisciplinaryMapper {
    @Select("SELECT d.*, s.name as student_name, s.student_number, " +
            "r.room_number, b.building_name " +
            "FROM disciplinary_records d " +
            "LEFT JOIN students s ON d.student_id = s.id " +
            "LEFT JOIN rooms r ON s.room_id = r.id " +
            "LEFT JOIN buildings b ON r.building_id = b.id " +
            "ORDER BY d.record_time DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "description", column = "description"),
        @Result(property = "recordTime", column = "record_time"),
        @Result(property = "student", javaType = com.dorm.entity.Student.class, 
            column = "student_id", 
            one = @One(select = "com.dorm.mapper.StudentMapper.findById"))
    })
    List<DisciplinaryRecord> findAll();

    @Select("SELECT d.*, s.name as student_name, s.student_number " +
            "FROM disciplinary_records d " +
            "LEFT JOIN students s ON d.student_id = s.id " +
            "WHERE d.student_id = #{studentId} " +
            "ORDER BY d.record_time DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "description", column = "description"),
        @Result(property = "recordTime", column = "record_time"),
        @Result(property = "student", javaType = com.dorm.entity.Student.class, 
            column = "student_id", 
            one = @One(select = "com.dorm.mapper.StudentMapper.findById"))
    })
    List<DisciplinaryRecord> findByStudentId(Long studentId);

    @Insert("INSERT INTO disciplinary_records(student_id, description, record_time) " +
            "VALUES(#{studentId}, #{description}, #{recordTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DisciplinaryRecord record);

    @Delete("DELETE FROM disciplinary_records WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT d.*, s.name as student_name, s.student_number " +
            "FROM disciplinary_records d " +
            "LEFT JOIN students s ON d.student_id = s.id " +
            "LEFT JOIN rooms r ON s.room_id = r.id " +
            "WHERE r.building_id = #{buildingId} " +
            "ORDER BY d.record_time DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "description", column = "description"),
        @Result(property = "recordTime", column = "record_time"),
        @Result(property = "student", javaType = com.dorm.entity.Student.class, 
            column = "student_id", 
            one = @One(select = "com.dorm.mapper.StudentMapper.findById"))
    })
    List<DisciplinaryRecord> findByBuildingId(Long buildingId);
} 